home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / patch12.arc / INP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-02  |  9.0 KB  |  341 lines

  1. /* $Header: inp.c,v 2.0.1.3 90/07/15 13:52:00 aas $
  2.  *
  3.  * $Log:    inp.c,v $
  4.  * Revision 2.0.1.3  90/07/15  13:52:00 aas
  5.  * include bugfix reported by mlord@bwdls58.bnr.ca in rev_in_string()
  6.  * regarding parameter string[] typo in first strnEQ() line
  7.  *
  8.  * Revision 2.0.1.2  90/05/30  11:30:00 mward
  9.  * if TURBOC20 have plan_a check against MAXMEMBLK before mallocs
  10.  * if TURBOC20 have plan_b use binary mode io for tmp file
  11.  * modified plan_a to allow for translation shrinkage on text reads
  12.  * modify calls to read, write to detect error on return value of -1
  13.  * 
  14.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  15.  * patch10: made a little smarter about sccs files
  16.  * 
  17.  * Revision 2.0  86/09/17  15:37:02  lwall
  18.  * Baseline for netwide release.
  19.  * 
  20.  */
  21.  
  22. #include "EXTERN.h"
  23. #include "common.h"
  24. #include "util.h"
  25. #include "pch.h"
  26. #include "INTERN.h"
  27. #include "inp.h"
  28.  
  29. /* Input-file-with-indexable-lines abstract type */
  30.  
  31. static long i_size;            /* size of the input file */
  32. static char *i_womp;            /* plan a buffer for entire file */
  33. static char **i_ptr;            /* pointers to lines in i_womp */
  34.  
  35. static int tifd = -1;            /* plan b virtual string array */
  36. static char *tibuf[2];            /* plan b buffers */
  37. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  38. static LINENUM lines_per_buf;        /* how many lines per buffer */
  39. static int tireclen;            /* length of records in tmp file */
  40.  
  41. /* New patch--prepare to edit another file. */
  42.  
  43. void
  44. re_input()
  45. {
  46.     if (using_plan_a) {
  47.     i_size = 0;
  48. #ifndef lint
  49.     if (i_ptr != Null(char**))
  50.         free((char *)i_ptr);
  51. #endif
  52.     if (i_womp != Nullch)
  53.         free(i_womp);
  54.     i_womp = Nullch;
  55.     i_ptr = Null(char **);
  56.     }
  57.     else {
  58.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  59.     Close(tifd);
  60.     tifd = -1;
  61.     free(tibuf[0]);
  62.     free(tibuf[1]);
  63.     tibuf[0] = tibuf[1] = Nullch;
  64.     tiline[0] = tiline[1] = -1;
  65.     tireclen = 0;
  66.     }
  67. }
  68.  
  69. /* Constuct the line index, somehow or other. */
  70.  
  71. void
  72. scan_input(filename)
  73. char *filename;
  74. {
  75.     if (!plan_a(filename))
  76.     plan_b(filename);
  77.     if (verbose) {
  78.     say3("Patching file %s using Plan %s...\n", filename,
  79.       (using_plan_a ? "A" : "B") );
  80.     }
  81. }
  82.  
  83. /* Try keeping everything in memory. */
  84.  
  85. bool
  86. plan_a(filename)
  87. char *filename;
  88. {
  89.     int ifd;
  90.     Reg1 char *s;
  91.     Reg2 LINENUM iline;
  92.  
  93.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  94.     if (verbose)
  95.         say2("(Creating file %s...)\n",filename);
  96.     makedirs(filename, TRUE);
  97.     close(creat(filename, 0666));
  98.     }
  99.     if (stat(filename, &filestat) < 0) {
  100.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  101.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  102.         Sprintf(buf, CHECKOUT, filename);
  103.         if (verbose)
  104.         say2("Can't find %s--attempting to check it out from RCS.\n",
  105.             filename);
  106.         if (system(buf) || stat(filename, &filestat))
  107.         fatal2("Can't check out %s.\n", filename);
  108.     }
  109.     else {
  110.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  111.         if (stat(s=buf+20, &filestat) >= 0 ||
  112.           stat(s=buf+25, &filestat) >= 0) {
  113.         Sprintf(buf, GET, s);
  114.         if (verbose)
  115.             say2("Can't find %s--attempting to get it from SCCS.\n",
  116.             filename);
  117.         if (system(buf) || stat(filename, &filestat))
  118.             fatal2("Can't get %s.\n", filename);
  119.         }
  120.         else
  121.         fatal2("Can't find %s.\n", filename);
  122.     }
  123.     }
  124.     filemode = filestat.st_mode;
  125.     if ((filemode & S_IFMT) & ~S_IFREG)
  126.     fatal2("%s is not a normal file--can't patch.\n", filename);
  127.     i_size = filestat.st_size;
  128.     if (out_of_mem) {
  129.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  130.     out_of_mem = FALSE;
  131.     return FALSE;            /* force plan b because plan a bombed */
  132.     }
  133.     i_womp = Nullch;
  134. #ifndef lint
  135. #ifdef TURBOC20
  136.     if (i_size < MAXMEMBLK)
  137. #endif
  138.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  139.                     /* i_size, but that's okay, I think. */
  140. #endif
  141.     if (i_womp == Nullch)
  142.     return FALSE;
  143.     if ((ifd = open(filename, 0)) < 0)
  144.     fatal2("Can't open file %s\n", filename);
  145. #ifndef lint
  146.     if ((i_size=(unsigned)read(ifd, i_womp, (int)i_size)) == (unsigned)-1) {
  147.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  148.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  149.     return FALSE;    /*   undersized. */
  150.     }
  151. #endif
  152.     Close(ifd);
  153.     if (i_size && i_womp[i_size-1] != '\n')
  154.     i_womp[i_size++] = '\n';
  155.     i_womp[i_size] = '\0';
  156.  
  157.     /* count the lines in the buffer so we know how many pointers we need */
  158.  
  159.     iline = 0;
  160.     for (s=i_womp; *s; s++) {
  161.     if (*s == '\n')
  162.         iline++;
  163.     }
  164.     i_ptr = Null(char**);
  165. #ifndef lint
  166. #ifdef TURBOC20
  167.     if (iline+2<MAXMEMBLK/sizeof(char *))
  168. #endif
  169.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  170. #endif
  171.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  172.     free((char *)i_womp);
  173.     return FALSE;
  174.     }
  175.     
  176.     /* now scan the buffer and build pointer array */
  177.  
  178.     iline = 1;
  179.     i_ptr[iline] = i_womp;
  180.     for (s=i_womp; *s; s++) {
  181.     if (*s == '\n')
  182.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  183.     }
  184.     input_lines = iline - 1;
  185.  
  186.     /* now check for revision, if any */
  187.  
  188.     if (revision != Nullch) { 
  189.     if (!rev_in_string(i_womp)) {
  190.         if (force) {
  191.         if (verbose)
  192.             say2(
  193. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  194.             revision);
  195.         }
  196.         else {
  197.         ask2(
  198. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  199.             revision);
  200.         if (*buf != 'y')
  201.         fatal1("Aborted.\n");
  202.         }
  203.     }
  204.     else if (verbose)
  205.         say2("Good.  This file appears to be the %s version.\n",
  206.         revision);
  207.     }
  208.     return TRUE;            /* plan a will work */
  209. }
  210.  
  211. /* Keep (virtually) nothing in memory. */
  212.  
  213. void
  214. plan_b(filename)
  215. char *filename;
  216. {
  217.     Reg3 FILE *ifp;
  218.     Reg1 int i = 0;
  219.     Reg2 int maxlen = 1;
  220.     Reg4 bool found_revision = (revision == Nullch);
  221.  
  222.     using_plan_a = FALSE;
  223.     if ((ifp = fopen(filename, "r")) == Nullfp)
  224.     fatal2("Can't open file %s\n", filename);
  225.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  226.     fatal2("Can't open file %s\n", TMPINNAME);
  227. #ifdef TURBOC20
  228.     setmode(tifd, O_BINARY);
  229. #endif
  230.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  231.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  232.         found_revision = TRUE;
  233.     if ((i = strlen(buf)) > maxlen)
  234.         maxlen = i;            /* find longest line */
  235.     }
  236.     if (revision != Nullch) {
  237.     if (!found_revision) {
  238.         if (force) {
  239.         if (verbose)
  240.             say2(
  241. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  242.             revision);
  243.         }
  244.         else {
  245.         ask2(
  246. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  247.             revision);
  248.         if (*buf != 'y')
  249.             fatal1("Aborted.\n");
  250.         }
  251.     }
  252.     else if (verbose)
  253.         say2("Good.  This file appears to be the %s version.\n",
  254.         revision);
  255.     }
  256.     Fseek(ifp, 0L, 0);        /* rewind file */
  257.     lines_per_buf = BUFFERSIZE / maxlen;
  258.     tireclen = maxlen;
  259.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  260.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  261.     if (tibuf[1] == Nullch)
  262.     fatal1("Can't seem to get enough memory.\n");
  263.     for (i=1; ; i++) {
  264.     if (! (i % lines_per_buf))    /* new block */
  265.         if (write(tifd, tibuf[0], BUFFERSIZE) == -1)
  266.         fatal1("patch: can't write temp file.\n");
  267.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  268.       == Nullch) {
  269.         input_lines = i - 1;
  270.         if (i % lines_per_buf)
  271.         if (write(tifd, tibuf[0], BUFFERSIZE) == -1)
  272.             fatal1("patch: can't write temp file.\n");
  273.         break;
  274.     }
  275.     }
  276.     Fclose(ifp);
  277.     Close(tifd);
  278. #ifdef TURBOC20
  279.     if ((tifd = open(TMPINNAME, O_BINARY)) < 0) {
  280. #else
  281.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  282. #endif
  283.     fatal2("Can't reopen file %s\n", TMPINNAME);
  284.     }
  285. }
  286.  
  287. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  288.  
  289. char *
  290. ifetch(line,whichbuf)
  291. Reg1 LINENUM line;
  292. int whichbuf;                /* ignored when file in memory */
  293. {
  294.     if (line < 1 || line > input_lines)
  295.     return "";
  296.     if (using_plan_a)
  297.     return i_ptr[line];
  298.     else {
  299.     LINENUM offline = line % lines_per_buf;
  300.     LINENUM baseline = line - offline;
  301.  
  302.     if (tiline[0] == baseline)
  303.         whichbuf = 0;
  304.     else if (tiline[1] == baseline)
  305.         whichbuf = 1;
  306.     else {
  307.         tiline[whichbuf] = baseline;
  308. #ifndef lint        /* complains of long accuracy */
  309.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  310. #endif
  311.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) == -1)
  312.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  313.     }
  314.     return tibuf[whichbuf] + (tireclen*offline);
  315.     }
  316. }
  317.  
  318. /* True if the string argument contains the revision number we want. */
  319.  
  320. bool
  321. rev_in_string(string)
  322. char *string;
  323. {
  324.     Reg1 char *s;
  325.     Reg2 int patlen;
  326.  
  327.     if (revision == Nullch)
  328.     return TRUE;
  329.     patlen = strlen(revision);
  330.     if (strnEQ(string,revision,patlen) && isspace(string[patlen]))
  331.     return TRUE;
  332.     for (s = string; *s; s++) {
  333.     if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  334.         isspace(s[patlen+1] )) {
  335.         return TRUE;
  336.     }
  337.     }
  338.     return FALSE;
  339. }
  340.  
  341.